layui.use(['form'], function() {
    var form = layui.form
        , $ = layui.$;
    //通过session获取修改类型和键值
    let courseData = JSON.parse(sessionStorage.getItem('courseData'));
    let changeData = JSON.parse(sessionStorage.getItem('changeData'));
    let user = JSON.parse(sessionStorage.getItem('user'));
    for(let key in courseData){
        if(key == 'state') continue;
        let html = `
         <tr ${key == 'id' ? 'hidden' : ''}>
            <td>${switchCompare(key)}</td>
            <td>${ key == 'createTime' ? util.getTime(courseData[key]) : courseData[key]}</td>
         </tr>
        `;
        $('#data').append(html);
    }
    for(let key in changeData){
        if(key == 'state') continue;
        let html = `
         <tr ${key == 'id' ? 'hidden' : ''}>
            <td>${switchCompare(key)}</td>
            <td>${ key == 'createTime' ? util.getTime(changeData[key]) : changeData[key]}</td>
         </tr>
        `;
        $('#datas').append(html);
    }
    //提交申请
    $('#btn').click(() =>{
        $.ajax({
            type: 'post',
            url: '/Class/apply/add',
            dataType: 'json',
            data: {
                "userId": user.id,
                "oldTid": courseData.id,
                "newTid": changeData.id,
                "type": parseInt(0),
                "startTime": new Date(),
                "state": parseInt(0),
            },
            success: (res) =>{
                if(res.code === 0){
                    layer.msg(res.msg);
                    sessionStorage.setItem('courseData', null);
                    sessionStorage.setItem('changeData', null);
                    let indexs = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(indexs);
                    window.parent.location.reload();
                }else layer.msg(res.msg);
            }
        })
    })
})
const courseField = {
    "courseId": '课程',
    "classId": '班级',
    "roomId": '教室',
    "userId": '教师',
    "schoolYear": '学年',
    "schoolTerm": '学期',
    "week": '第几周',
    "weekNum": '周几',
    "timeSlot": '节数',
    "createTime": '课程时间'
}
function switchCompare(key) {
    for (let field in courseField){
        if (key == field){
            return courseField[field];
        }
    }
}